package com.example.pplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TaskManager {
    private static final String TAG = "TaskManager";
    private final DBHelper dbHelper;
    private long currentUserId;

    public TaskManager(Context context, long userId) {
        dbHelper = new DBHelper(context);
        this.currentUserId = userId;
    }

    // 添加新任务
    public long addTask(String title, String description, String priority,
                        String category, String dueDate, String reminderTime) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_TITLE, title);
        values.put(DBHelper.COLUMN_DESCRIPTION, description);
        values.put(DBHelper.COLUMN_PRIORITY, priority);
        values.put(DBHelper.COLUMN_CATEGORY, category);
        values.put(DBHelper.COLUMN_DUE_DATE, dueDate);
        values.put(DBHelper.COLUMN_REMINDER_TIME, reminderTime);
        values.put(DBHelper.COLUMN_USER_ID, currentUserId);

        try {
            long id = db.insert(DBHelper.TASK_TABLE, null, values);
            Log.d(TAG, "添加任务成功, ID: " + id);
            return id;
        } catch (Exception e) {
            Log.e(TAG, "添加任务失败", e);
            return -1;
        } finally {
            db.close();
        }
    }

    // 根据过滤器获取任务
    public Cursor getTasksByFilter(String filter) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String selection = DBHelper.COLUMN_USER_ID + " = ?";
        String[] selectionArgs = {String.valueOf(currentUserId)};
        String orderBy = DBHelper.COLUMN_PRIORITY + " DESC, " +
                DBHelper.COLUMN_DUE_DATE + " ASC";

        switch (filter) {
            case "today":
                String today = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
                selection += " AND " + DBHelper.COLUMN_DUE_DATE + " = ? AND " +
                        DBHelper.COLUMN_IS_COMPLETED + " = 0";
                selectionArgs = new String[]{String.valueOf(currentUserId), today};
                break;
            case "high_priority":
                selection += " AND " + DBHelper.COLUMN_PRIORITY + " = ? AND " +
                        DBHelper.COLUMN_IS_COMPLETED + " = 0";
                selectionArgs = new String[]{String.valueOf(currentUserId), "优先"};
                break;
            case "completed":
                selection += " AND " + DBHelper.COLUMN_IS_COMPLETED + " = 1";
                break;
            case "all":
                // 不添加额外条件，显示所有任务
                break;
            default: // 默认只显示未完成的任务
                selection += " AND " + DBHelper.COLUMN_IS_COMPLETED + " = 0";
                break;
        }

        return db.query(DBHelper.TASK_TABLE,
                null,
                selection,
                selectionArgs,
                null, null,
                orderBy);
    }

    // 标记任务为已完成
    public boolean markTaskCompleted(long taskId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_IS_COMPLETED, 1);

        String whereClause = DBHelper.COLUMN_ID + " = ? AND " +
                DBHelper.COLUMN_USER_ID + " = ?";
        String[] whereArgs = {String.valueOf(taskId), String.valueOf(currentUserId)};

        try {
            int rows = db.update(DBHelper.TASK_TABLE, values, whereClause, whereArgs);
            return rows > 0;
        } finally {
            db.close();
        }
    }

    // 删除任务
    public boolean deleteTask(long taskId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String whereClause = DBHelper.COLUMN_ID + " = ? AND " +
                DBHelper.COLUMN_USER_ID + " = ?";
        String[] whereArgs = {String.valueOf(taskId), String.valueOf(currentUserId)};

        try {
            int rows = db.delete(DBHelper.TASK_TABLE, whereClause, whereArgs);
            Log.d(TAG, "删除任务, 影响行数: " + rows);
            return rows > 0;
        } catch (Exception e) {
            Log.e(TAG, "删除任务失败", e);
            return false;
        } finally {
            db.close();
        }
    }

    // 获取任务详情
    public Cursor getTaskDetails(long taskId) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String selection = DBHelper.COLUMN_ID + " = ? AND " +
                DBHelper.COLUMN_USER_ID + " = ?";
        String[] selectionArgs = {String.valueOf(taskId), String.valueOf(currentUserId)};

        return db.query(DBHelper.TASK_TABLE,
                null,
                selection,
                selectionArgs,
                null, null, null);
    }

    // 更新任务
    public boolean updateTask(long taskId, String title, String description,
                              String priority, String category, String dueDate,
                              String reminderTime) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_TITLE, title);
        values.put(DBHelper.COLUMN_DESCRIPTION, description);
        values.put(DBHelper.COLUMN_PRIORITY, priority);
        values.put(DBHelper.COLUMN_CATEGORY, category);
        values.put(DBHelper.COLUMN_DUE_DATE, dueDate);
        values.put(DBHelper.COLUMN_REMINDER_TIME, reminderTime);

        String whereClause = DBHelper.COLUMN_ID + " = ? AND " +
                DBHelper.COLUMN_USER_ID + " = ?";
        String[] whereArgs = {String.valueOf(taskId), String.valueOf(currentUserId)};

        try {
            int rows = db.update(DBHelper.TASK_TABLE, values, whereClause, whereArgs);
            return rows > 0;
        } finally {
            db.close();
        }
    }
}